//
//  MJAlertView.m
//  OrderSys
//
//  Created by JM on 15/10/11.
//  Copyright © 2015年 zjlskj. All rights reserved.
//

#import "MJAlertView.h"

@interface MJAlertView()

@property(assign) id target;
@property(assign) SEL clickedSel;

#if NS_BLOCKS_AVAILABLE
@property(copy) SNTAlertViewClickedBlock clickedBlock;
#endif

@end

@implementation MJAlertView
@synthesize target, clickedSel;

#if NS_BLOCKS_AVAILABLE
@synthesize clickedBlock;
#endif

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
#if !(__has_feature(objc_arc))
    [super dealloc];
#endif
}

+ (id)showAlertViewWithTitle:(NSString*)title
                     message:(NSString*)message
                      target:(id)target
       clickedButtonSelector:(SEL)clickedButtonSelector
           cancelButtonTitle:(NSString*)cancelButtonTitle
           otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    MJAlertView *alertView = [[[self class] alloc] initWithTitle:title
                                                         message:message
                                                        delegate:nil
                                               cancelButtonTitle:cancelButtonTitle
                                               otherButtonTitles:nil];
    alertView.delegate = alertView;
    alertView.target = target;
    alertView.clickedSel = clickedButtonSelector;
    
    id eachObject;
    va_list argumentList;
    if (otherButtonTitles) {
        [alertView addButtonWithTitle: otherButtonTitles];
        va_start(argumentList, otherButtonTitles);
        
        while ((eachObject = va_arg(argumentList, id)) != nil) {
            [alertView addButtonWithTitle: eachObject];
        }
        
        va_end(argumentList);
    }
    
    [alertView show];
    return alertView;
}

#if NS_BLOCKS_AVAILABLE

+ (id)showAlertViewWithTitle:(NSString*)title
                     message:(NSString*)message
          clickedButtonBlock:(SNTAlertViewClickedBlock)clickedButtonBlock
           cancelButtonTitle:(NSString*)cancelButtonTitle
           otherButtonTitles:(NSString*)otherButtonTitles, ...
{
    MJAlertView *alertView = [[[self class] alloc] initWithTitle:title
                                                         message:message
                                                        delegate:nil
                                               cancelButtonTitle:cancelButtonTitle
                                               otherButtonTitles:nil];
    alertView.delegate = alertView;
    alertView.clickedBlock = clickedButtonBlock;
    
    id eachObject;
    va_list argumentList;
    if (otherButtonTitles) {
        [alertView addButtonWithTitle: otherButtonTitles];
        va_start(argumentList, otherButtonTitles);
        
        while ((eachObject = (id) va_arg(argumentList, id)) != nil) {
            [alertView addButtonWithTitle: eachObject];
        }
        
        va_end(argumentList);
    }
    
    [alertView show];
    
    return alertView;
}

#endif

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (target && clickedSel) {
        NSMethodSignature *sig = [target methodSignatureForSelector:clickedSel];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
        [invocation setSelector:clickedSel];
        [invocation setTarget:target];
        [invocation setArgument:&buttonIndex atIndex:2];
        [invocation invoke];
    }
#if NS_BLOCKS_AVAILABLE
    if (self.clickedBlock) {
        self.clickedBlock(buttonIndex);
    }
#endif
}

//注册自己，监听应用进入后台的事件
- (void) show
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];
    
    [super show];
}

//进入后台的回调
//warning:cancel按钮必须是个取消按钮，而不能是任何业务提交按钮
//- (void) applicationDidEnterBackground:(NSNotification*) notification
//{
//    //    NSLog(@">> SNTAlertView applicationDidEnterBackground");
//    
//    [self dismissWithClickedButtonIndex:[self cancelButtonIndex] animated:NO];
//}


@end
